package com.uton.rfidcontroller.view.activity.write;

import android.content.Intent;

import com.uton.rfidcontroller.R;
import com.uton.rfidcontroller.common.GlobalConstants;
import com.uton.rfidcontroller.common.intent.IntentBuilder;
import com.uton.rfidcontroller.common.utils.Utils;
import com.uton.rfidcontroller.databinding.ActivityWriteDetailBinding;
import com.uton.rfidcontroller.view.activity.CarDetailActivity;
import com.uton.rfidcontroller.view.activity.write.prescan.PreScanToWriteActivity;
import com.uton.rfidcontroller.view.base.BaseActivity;
import com.uton.rfidcontroller.viewmodel.activity_model.WriteDetailActivityViewModel;

import io.reactivex.ObservableTransformer;

/**
 * @author 王家宝
 * @date 2017/10/16
 * Email:639586819@qq.com
 * Tel:18241150668
 */

public class WriteDetailActivity extends BaseActivity<ActivityWriteDetailBinding, WriteDetailActivityViewModel> {
    public static final String PRE_WRITE_IN = "1";
    public static final String ALREADY_WRITE_IN = "2";
    /**
     * 判断最后一次写入是否成功
     */
    private boolean isWriteInSuccess;
    private String tidNum;

    @Override
    protected int setLayout() {
        return R.layout.activity_write_detail;
    }

    @Override
    protected void inject() {
        getBinding().setModel(getViewModel());
    }

    @Override
    protected String setTitle() {
        return getString(R.string.ui_write);
    }

    @Override
    protected void doSomething() {
        String bindState = getIntent().getStringExtra(GlobalConstants.EXTRA_STRING_PARAM3);
        if (PRE_WRITE_IN.equals(bindState)) {
            getViewModel().isWriteIn.set(false);
        } else if (ALREADY_WRITE_IN.equals(bindState)) {
            getViewModel().isWriteIn.set(true);
        } else {
            getViewModel().isWriteIn.set(true);
        }
    }

    @Override
    protected WriteDetailActivityViewModel setViewModel() {
        return new WriteDetailActivityViewModel(
                getIntent().getStringExtra(GlobalConstants.EXTRA_STRING_PARAM1),
                getIntent().getStringExtra(GlobalConstants.EXTRA_STRING_PARAM2)) {
            @Override
            public void showCarDetail() {
                startActivity(new IntentBuilder().from(WriteDetailActivity.this, CarDetailActivity.class).
                        setStringParam(GlobalConstants.EXTRA_STRING_PARAM1, getIntent().
                                getStringExtra(GlobalConstants.EXTRA_STRING_PARAM1)).
                        getIntent());
            }

            @Override
            public void toWrite() {
                /**
                 * 第二个参数：要写入的RFID标签ID
                 */
                startActivityForResult(new IntentBuilder().from(WriteDetailActivity.this, PreScanToWriteActivity.class).
                        setIntParam(GlobalConstants.EXTRA_INT_PARAM1, GlobalConstants.RFID_WRITE).
                        setStringParam(GlobalConstants.EXTRA_STRING_PARAM1, getViewModel().getRfidLabelId()).
                        getIntent(), GlobalConstants.REQUEST_WANT_DATA);
            }

            @Override
            public void toRead() {
                startActivity(new IntentBuilder().from(WriteDetailActivity.this, PreScanToWriteActivity.class).
                        setIntParam(GlobalConstants.EXTRA_INT_PARAM1, GlobalConstants.RFID_READ).
                        getIntent());
            }

            @Override
            public void doOnSuccess() {
                setResult(GlobalConstants.RESULT_COMMIT_SUCCESS);
                finish();
            }

            @Override
            public void showDialog() {
                showProgressDialog(null, false);
            }

            @Override
            public void dismissDialog() {
                disDialog();
            }

            @Override
            public void doOnComplete() {
            }

            @Override
            public boolean isWriteInSuccess() {
                return isWriteInSuccess;
            }

            @Override
            public boolean isTidGetSuccess() {
                return tidNum != null && tidNum.length() != 0;
            }

            @Override
            public void showCheckErrorMsg() {
                Utils.showShortToast("该电子标签最后一次写入失败或未获取到TID码，不能提交");
            }

            @Override
            public String getTid() {
                return tidNum;
            }

            @Override
            public ObservableTransformer setObservable() {
                return compose(bindToLifecycle());
            }
        };
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case GlobalConstants.RESULT_IS_WRITE_SUCCESS:
                isWriteInSuccess = data.getBooleanExtra(GlobalConstants.IS_WRITE_IN_SUCCESS, false);
                tidNum = data.getStringExtra(GlobalConstants.TID_NUM);
                break;
            default:
                break;
        }
    }
}

